<?php
namespace app\index\service;


use app\index\model\DealcomModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouModel;
use app\index\model\PiaoTaxModel;

class PiaoService extends CommonService
{


    public function get_kai_piaotype(){
        return [
            1=>'增值税专用发票',
            2=>'增值税普通发票',
        ];
    }
    public function get_kai_piaotype_name($piaotype){
        $arr = $this->get_kai_piaotype();
        $str = isset($arr[$piaotype]) ? $arr[$piaotype] : '';
        return $str;
    }

    public function get_shou_piaotype(){
        return [
            1=>'增值税专用发票',
            2=>'增值税普通发票',
        ];
    }
    public function get_shou_piaotype_name($piaotype){
        $arr = $this->get_shou_piaotype();
        $str = isset($arr[$piaotype]) ? $arr[$piaotype] : '';
        return $str;
    }



    public function create_piao_tax_number(){
        $tou = 'SFYJ';
        $daystr = substr(date("Ymd"), 2);
        $number = PiaoTaxModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }



    public function create_piao_kai_number(){
        $tou = 'KPDJ';
        $daystr = substr(date("Ymd"), 2);
        $number = PiaokaiModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_piao_shou_number(){
        $tou = 'SPDJ';
        $daystr = substr(date("Ymd"), 2);
        $number = PiaoShouModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function get_piaoshou_trip_info($piaoPath){

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "https://dgfp.market.alicloudapi.com/ocrservice/invoice");
        curl_setopt($curl, CURLOPT_POST,true);

        $host = "https://dgfp.market.alicloudapi.com/ocrservice/invoice";

        $method = "POST";
        $appcode = "93cf41411f81447188675ade3c2fab3f";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
        $querys = "";
        $bodys = "{\"url\":\"".$piaoPath."\"}";

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $host);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $out_put = curl_exec($curl);


        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);


        list($header, $body) = explode("

", $out_put, 2);
        if ($httpCode == 200) {
            $res = json_decode($body, true);

            $info['gonger'] = '';
            if(isset($res['data']['销售方名称'])){
                $dealRes = DealcomModel::where(['comid'=>$this->userinfo['comid'],
                    'deleted_user'=>0,'isturn'=>1,'status'=>1,'name'=>$res['data']['销售方名称']])->find();
                if($dealRes){
                    $info['gonger'] = $dealRes['name'];
                }
            }

            $info['dt'] = isset($res['data']['开票日期']) ? $res['data']['开票日期'] : '';
            $info['dt'] = str_replace('年', '-', $info['dt']);
            $info['dt'] = str_replace('月', '-', $info['dt']);
            $info['dt'] = str_replace('日', '', $info['dt']);

            $info['nums'] = isset($res['data']['发票号码']) ? $res['data']['发票号码'] : '';
            $info['piaotype'] = '';
            if(isset($res['data']['发票类型'])){
                if($res['data']['发票类型'] == '专用发票'){
                    $info['piaotype'] = 1;
                }elseif($res['data']['发票类型'] == '普通发票'){
                    $info['piaotype'] = 2;
                }
            }
            $info['fee'] = isset($res['data']['不含税金额']) ? $res['data']['不含税金额'] : '';
            $info['rate'] = isset($res['data']['发票详单']) && isset($res['data']['发票详单'][0]['税率']) ? $res['data']['发票详单'][0]['税率'] : '';
            $info['rate'] = str_replace('%', '', $info['rate']);
            $info['ratefee'] = isset($res['data']['发票税额']) ? $res['data']['发票税额'] : '';
            $info['money'] = isset($res['data']['发票金额']) ? $res['data']['发票金额'] : '';
            $info['explain'] = isset($res['data']['货物或应税劳务、服务名称']) ? $res['data']['货物或应税劳务、服务名称'] : '';
            $info['explain'] = '';
            if($res['data']['发票详单']){
                foreach ($res['data']['发票详单'] as $vvv){
                    $info['explain'] .= $vvv['货物或应税劳务、服务名称'].'；';
                }
            }
            return ['code'=>2000, 'data'=>$info];
        } else {
            return ['code'=>4000, 'data'=>[]];
        }
    }

    public function get_piaokai_trip_info($piaoPath){

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "https://dgfp.market.alicloudapi.com/ocrservice/invoice");
        curl_setopt($curl, CURLOPT_POST,true);

        $host = "https://dgfp.market.alicloudapi.com/ocrservice/invoice";

        $method = "POST";
        $appcode = "93cf41411f81447188675ade3c2fab3f";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
        $querys = "";
        $bodys = "{\"url\":\"".$piaoPath."\"}";

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $host);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $out_put = curl_exec($curl);


        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);


        list($header, $body) = explode("

", $out_put, 2);
        if ($httpCode == 200) {
            $res = json_decode($body, true);


            $info['gonger'] = isset($res['data']['受票方名称']) ? $res['data']['受票方名称'] : '';
            $info['nums'] = isset($res['data']['发票号码']) ? $res['data']['发票号码'] : '';
            $info['fee'] = isset($res['data']['不含税金额']) ? $res['data']['不含税金额'] : '';
            $info['rate'] = isset($res['data']['发票详单']) && isset($res['data']['发票详单'][0]['税率']) ? $res['data']['发票详单'][0]['税率'] : '';
            $info['rate'] = str_replace('%', '', $info['rate']);
            $info['ratefee'] = isset($res['data']['发票税额']) ? $res['data']['发票税额'] : '';
            $info['money'] = isset($res['data']['发票金额']) ? $res['data']['发票金额'] : '';
            $info['explain'] = isset($res['data']['货物或应税劳务、服务名称']) ? $res['data']['货物或应税劳务、服务名称'] : '';
            $info['explain'] = '';
            if($res['data']['发票详单']){
                foreach ($res['data']['发票详单'] as $vvv){
                    $info['explain'] .= $vvv['货物或应税劳务、服务名称'].'；';
                }
            }
            return ['code'=>2000, 'data'=>$info];
        } else {
            return ['code'=>4000, 'data'=>[]];
        }
    }




}
